/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummy = new ListNode(0, head);
        ListNode p, q = dummy, r;
        while (q.next != null && q.next.next != null){
            p = q;
            q = q.next;
            r = q.next;
            q.next = r.next;
            r.next = q;
            p.next = r;
        }
        return dummy.next;
    }
}